#include "../ui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdbool.h>

#include <fcntl.h>

static bool btn_pressed = false; // 用于跟踪按钮状态

// 定时器回调函数，用于重置按钮状态
static void reset_btn_pressed(lv_timer_t *timer)
{
  btn_pressed = false; // 重置按钮状态
  lv_timer_del(timer); // 删除定时器
}

static FILE *fp;
static int b, c, z, a, y, x;
static double xx, yy, zz, aa;
static lv_timer_t *up_joystick_timer;

lv_obj_t *x_bar;
lv_obj_t *y_bar;
lv_obj_t *z_bar;
lv_obj_t *a_bar;

volatile int joystick_running = 0;
volatile int wifi_state_flag = 0;
pthread_t joystick_thread;

static void get_joystick()
{
  while(joystick_running)
  {
  // fp = popen("cat /sys/bus/iio/devices/iio:device0/in_voltage*_raw","r");
  fp = popen("cat /sys/bus/iio/devices/iio:device0/in_voltage2_raw \
                  /sys/bus/iio/devices/iio:device0/in_voltage3_raw \
                  /sys/bus/iio/devices/iio:device0/in_voltage4_raw \
                  /sys/bus/iio/devices/iio:device0/in_voltage5_raw",
             "r");
  if (fp == NULL)
  {
    printf("Open File Failed.\n");
    return;
  }
  if (fscanf(fp, "%d %d %d %d", &z, &a, &y, &x) != 4)
  {
    printf("Failed to read all values.\n");
    pclose(fp);
    return;
  }
  pclose(fp);

  printf("b: %d, c: %d, z: %d, a: %d, y: %d, x: %d\n", b, c, z, a, y, x);

  // 量化至0~100
  yy = 100.0 * (((double)(y - 252) / (745.0 - 252)));
  xx = 100.0 * (((double)(x - 263) / (758.0 - 263)));
  zz = 100.0 * (((double)(z - 69) / (984.0 - 69)));
  aa = 100.0 * (((double)(a - 156) / (863.0 - 156)));

  yy = 100 - yy;
  zz = 100 - zz;
  printf("z: %f, a: %f, y: %f, x: %f\n", zz, aa, yy, xx);

  lv_bar_set_value(x_bar, xx, LV_ANIM_ON);
  lv_bar_set_value(y_bar, yy, LV_ANIM_ON);
  lv_bar_set_value(z_bar, zz, LV_ANIM_ON);
  lv_bar_set_value(a_bar, aa, LV_ANIM_ON);

  if(wifi_state_flag == 1)
  {
      //Push至服务器
    char tbf[1024];
    sprintf(tbf,"wget -q -O - \"http://192.168.173.19:5000/upload?x=%lf&y=%lf&a=%lf&z=%lf\"",yy,xx,zz,aa);
    // wget -q -O - "http://192.168.73.19:5000/upload?x=0.0&y=0.0&a=0.0&z=0.0"
    system(tbf);
  }

  }
}

static before_switch_screen(void)
{
  // lv_timer_del(up_joystick_timer);
  if(joystick_running!=0)
  {
    joystick_running = 0;
    pthread_join(joystick_thread, NULL);
  }
  
}

static void btn_event_cb(lv_event_t *e)
{
  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t *btn = lv_event_get_target(e);
  uint32_t id = lv_btnmatrix_get_selected_btn(btn);
  if (code == LV_EVENT_VALUE_CHANGED)
  {
    if (!btn_pressed) // 检查按钮是否已经被按下
    {
      btn_pressed = true; // 标记按钮已被按下
      lv_timer_create(reset_btn_pressed, 100, NULL);
      
      const char *btn_text = lv_btnmatrix_get_btn_text(btn, lv_btnmatrix_get_selected_btn(btn));

      if (strcmp(btn_text, "返回") == 0) /* 切换到功能1界面 */
      {
        before_switch_screen();
        ui_Home_screen_init();
        lv_scr_load_anim(ui_Home, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, true);
      }

      if (strcmp(btn_text, "Start") == 0) /* 切换到功能1界面 */
      {
        if(joystick_running) return; //如果已经在运行就返回

        if (get_wifi_state())
        {
          joystick_running = 1;
          wifi_state_flag = 1;
          pthread_create(&joystick_thread, NULL, get_joystick, NULL);
        }
        else
        {
          info_msgbox("\nWiFi is disconnected\n           Test Mode","Error");
          joystick_running = 1;
          wifi_state_flag = 0;
          pthread_create(&joystick_thread, NULL, get_joystick, NULL);
        }
      }

      
    }
  }
}

static void lv_example_bar_2(lv_obj_t *parent)
{
  static lv_style_t style_bg;
  static lv_style_t style_indic;

  lv_style_init(&style_bg);
  lv_style_set_border_color(&style_bg, lv_palette_main(LV_PALETTE_BLUE));
  lv_style_set_border_width(&style_bg, 2);
  lv_style_set_pad_all(&style_bg, 6); /*To make the indicator smaller*/
  lv_style_set_radius(&style_bg, 6);
  lv_style_set_anim_time(&style_bg, 1000);

  lv_style_init(&style_indic);
  lv_style_set_bg_opa(&style_indic, LV_OPA_COVER);
  lv_style_set_bg_color(&style_indic, lv_palette_main(LV_PALETTE_BLUE));
  lv_style_set_radius(&style_indic, 3);

  x_bar = lv_bar_create(parent);
  lv_obj_remove_style_all(x_bar); /*To have a clean start*/
  lv_obj_add_style(x_bar, &style_bg, 0);
  lv_obj_add_style(x_bar, &style_indic, LV_PART_INDICATOR);
  lv_obj_set_size(x_bar, 200, 20);
  // lv_obj_center(x_bar);
  lv_obj_align(x_bar, LV_ALIGN_CENTER, 0, 0);

  y_bar = lv_bar_create(parent);
  lv_obj_remove_style_all(y_bar); /*To have a clean start*/
  lv_obj_add_style(y_bar, &style_bg, 0);
  lv_obj_add_style(y_bar, &style_indic, LV_PART_INDICATOR);
  lv_obj_set_size(y_bar, 200, 20);
  // lv_obj_center(y_bar);
  lv_obj_align(y_bar, LV_ALIGN_CENTER, 0, -30);

  z_bar = lv_bar_create(parent);
  lv_obj_remove_style_all(z_bar); /*To have a clean start*/
  lv_obj_add_style(z_bar, &style_bg, 0);
  lv_obj_add_style(z_bar, &style_indic, LV_PART_INDICATOR);
  lv_obj_set_size(z_bar, 200, 20);
  // lv_obj_center(z_bar);
  lv_obj_align(z_bar, LV_ALIGN_CENTER, 0, -60);

  a_bar = lv_bar_create(parent);
  lv_obj_remove_style_all(a_bar); /*To have a clean start*/
  lv_obj_add_style(a_bar, &style_bg, 0);
  lv_obj_add_style(a_bar, &style_indic, LV_PART_INDICATOR);
  lv_obj_set_size(a_bar, 200, 20);
  // lv_obj_center(a_bar);
  lv_obj_align(a_bar, LV_ALIGN_CENTER, 0, -90);

  lv_bar_set_value(x_bar, 100, LV_ANIM_ON);
  lv_bar_set_value(y_bar, 100, LV_ANIM_ON);
  lv_bar_set_value(z_bar, 100, LV_ANIM_ON);
  lv_bar_set_value(a_bar, 100, LV_ANIM_ON);
}

extern unsigned int back_ground_value;

static void create_function_widgets(lv_obj_t *parent)
{
  // 设置背景颜色
  lv_obj_set_style_bg_color(parent, lv_color_hex(back_ground_value), 0);

  // 创建底部按钮
  static const char *btn_map[] = {"Start", "返回", ""};
  lv_obj_t *btnm = lv_btnmatrix_create(parent);
  lv_btnmatrix_set_map(btnm, btn_map);
  lv_obj_set_size(btnm, lv_pct(90), lv_pct(25));
  lv_obj_align(btnm, LV_ALIGN_BOTTOM_MID, 0, -40);
  lv_obj_add_event_cb(btnm, btn_event_cb, LV_EVENT_ALL, NULL);

  // 设置按钮样式
  lv_obj_set_style_text_font(btnm, &lv_font_simsun_16_cjk, 0);
  // lv_obj_set_style_bg_color(btnm, lv_color_hex(0x34495e), LV_PART_MAIN);
  lv_obj_set_style_bg_color(btnm, lv_color_hex(0xDBDBDB), LV_PART_MAIN);
  lv_obj_set_style_border_width(btnm, 0, LV_PART_MAIN);
  lv_obj_set_style_radius(btnm, 10, LV_PART_MAIN);
  lv_obj_set_style_text_color(btnm, lv_color_hex(0xecf0f1), LV_PART_ITEMS);
  lv_obj_set_style_bg_color(btnm, lv_color_hex(0x3498db), LV_PART_ITEMS);
  lv_obj_set_style_radius(btnm, 5, LV_PART_ITEMS);

  lv_example_bar_2(parent);

  // static const char *app_btn_map[] = {"Joystick","Void", "Void", "\n", "Void", "Void", "Void",  ""};
  // lv_obj_t *app_btn = lv_btnmatrix_create(parent);
  // lv_btnmatrix_set_map(app_btn, app_btn_map);
  // lv_obj_set_size(app_btn, lv_pct(90), lv_pct(50));
  // lv_obj_align(app_btn, LV_ALIGN_CENTER, 0, -50);
  // lv_obj_add_event_cb(app_btn, btn_event_cb, LV_EVENT_ALL, NULL);

  // // lv_obj_set_style_bg_color(app_btn, lv_color_hex(0xDBDBDB), LV_PART_MAIN);
  // lv_obj_set_style_border_width(app_btn, 2, LV_PART_MAIN);
  // lv_obj_set_style_radius(app_btn, 10, LV_PART_MAIN);
  // lv_obj_set_style_text_color(app_btn, lv_color_hex(0xecf0f1), LV_PART_ITEMS);
  // lv_obj_set_style_bg_color(app_btn, lv_color_hex(0x3498db), LV_PART_ITEMS);
  // lv_obj_set_style_radius(app_btn, 5, LV_PART_ITEMS);
}

void ui_Sensor_screen_init(void)
{
  ui_Sensor = lv_obj_create(NULL);
  lv_obj_clear_flag(ui_Sensor, LV_OBJ_FLAG_SCROLLABLE); /// Flags
  create_function_widgets(ui_Sensor);
}
